package hui.zhang.web.action.base;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import hui.zhang.domain.base.Standard;
import hui.zhang.service.base.StandardService;

/**
 * 收派标准表现层
 * @author hui.zhang
 * @date 2017年12月9日 下午6:40:40
 */

@ParentPackage("struts-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {

	//模型驱动封装
	private Standard standard = new Standard();
	@Override
	public Standard getModel() {
		return standard;
	}
	
	//注入service
	@Autowired
	private StandardService standardService;
	
	/**
	 * 添加、修改 收派标准信息
	 * @return
	 */
	@Action(value="standard_save",results={
			@Result(name="success",type="redirect",location="pages/base/standard.html")})
	public String save() {
		standardService.save(standard);
		return SUCCESS;
	}
	
	//属性封装前台datagird异步传来的分页信息
	private Integer page;
	private Integer rows;
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	/**
	 * 分页显示所有收派标准
	 * @return
	 */
	@Action(value="standard_pageQuery")//,results={@Result(name="success")}
	public void findAllByPage() {
		//page从0查询
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> list = standardService.findAllByPage(pageable);
		long totalElements = list.getTotalElements();
		List<Standard> beanList = list.getContent();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", totalElements);
		map.put("rows", beanList);
		String jsonString = JSONArray.toJSONString(map);
		try {
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(jsonString);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 查询所有
	 */
	@Action(value="standard_findAll")
	public void findAll() {
		try {
			ServletActionContext.getRequest().setCharacterEncoding("utf-8");
			List<Standard> list = standardService.findAll();
			String string = JSON.toJSONString(list);
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(string);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
